
#=============================================================================#
#
# This R file contains the code used to get descriptive statistics
# (in order of appearance in the manuscript)
# 
#=============================================================================#

# Initial settings ------------------------------------------------------------

rm(list=ls())
getwd()

# Install and load all necessary packages with the ipak function:
# i.e., check to see if packages are installed. 
# Install them if they are not, 
# then load them into the R session.

ipak <- function(pkg){  new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if(length(new.pkg)) install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}

packages <- c("readxl", "dplyr") 
ipak(packages)


# Data ------------------------------------------------------------------------
df <- readRDS("data/df.rds") 
# delete 3 studies on effects of terrorism conducted outside Western democracies 
# extra criterion 3
df <- df %>% 
  filter((ExactAttack_2 != "2002 Bali bombings" &  # attacks on western countries 
            ExactAttack_2 != "2008 Mumbai attacks" &  # attacks on western countries 
            ExactAttack_2 != "Gaza - Israel conflict") %>%
           replace_na(TRUE))


# Descriptives ----------------------------------------------------------------
df %>% summarize(min = min(Year), max = max(Year)) # publication year: range

df %>% distinct(ID_ES_Unique) %>% nrow() # n effect sizes

df %>% distinct(ID_R) %>% nrow() # n manuscripts

df %>% filter(ajps == 0) %>% distinct(ID_R) %>% nrow() # new manuscripts

df %>% distinct(ID_RS) %>% nrow() # n studies

df %>% 
  summarize(
    min = min(StudyYear, na.rm = TRUE),
    max = max(StudyYear, na.rm = TRUE)
  ) # study year: range

sort(unique(df$Country)) # list of countries

df %>% count(TypeStudy) %>% mutate( prop = n / n_distinct(df)) # research designs

summary(as.factor(df$PA_Category)) # outcome categories following Godefroidt (2023)

selected_categories <- c('10', '99') # outgroup hostility 
num_observations <- sum(df$PA_Category %in% selected_categories, na.rm = TRUE)
num_observations / nrow(df)

selected_categories <- c('5', '6', '7', '8', '9', '11') # conservative shift
num_observations <- sum(df$PA_Category %in% selected_categories, na.rm = TRUE)
num_observations / nrow(df)

selected_categories <- c('1', '2', '3', '4') # rally and activation effects
num_observations <- sum(df$PA_Category %in% selected_categories, na.rm = TRUE)
num_observations / nrow(df)


# Additional results (not reported in paper)
selected_categories <- c('5', '6', '7', '11') # generic conservative shift
num_observations <- sum(df$PA_Category %in% selected_categories, na.rm = TRUE)
num_observations / nrow(df)

selected_categories <- c('8', '9') # policy preferences
num_observations <- sum(df$PA_Category %in% selected_categories, na.rm = TRUE)
num_observations / nrow(df)

selected_categories <- c('3') # activation effects
num_observations <- sum(df$PA_Category %in% selected_categories, na.rm = TRUE)
num_observations / nrow(df)

selected_categories <- c('5') # RWA
num_observations <- sum(df$PA_Category %in% selected_categories, na.rm = TRUE)
num_observations / nrow(df)

selected_categories <- c('6') # SDO
num_observations <- sum(df$PA_Category %in% selected_categories, na.rm = TRUE)
num_observations / nrow(df)


#### Category of the independent variable (different, more appropriate, categorization)
#* 1 = Nationalism, patriotism, national pride, national identification, system justification
#* 2 = Political and institutional trust (both national and international)
#* 3 = Political participation, incl. voter turnout
#* 4 = Rally around a leader/politician
#* 5 = RWA or other measures of authoritarianism/cultural conservatism
#* 6 = SDO or other measures of socio-economic conservatism
#* 7 = General measures of political ideology
#* 8 = Support for hawkish/military policies (as opposed to diplomatic solutions, such as support for peace agreements or compromises, etc.)
#* 9 = Support for policies related to civil liberties/privacy/torture
#* 10 = Support for policies pertaining an outgroup (e.g., immigration policies)
#* 11 = Other (unclear, mixed, etc.)
#* 99 = Outgroup attitudes (without political component)